package cn.yuhh.api.helper;

import cn.yuhh.core.type.ResponseHeader;

import java.util.*;

/**
 * @author ysw
 * @create 2019-02-24 21:55
 * @description
 **/
public class ApiResponse {

    public static List dataToMaps(Collection source, IDataToMap dataToMap) {
        if (source == null || source.isEmpty()) {
            return  new ArrayList();
        } else {
            List list = new ArrayList();
            for (Object obj : source) {
                Map map = new HashMap();
                try {
                    dataToMap.dataToMap(map, obj);
                    list.add(map);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            return list;
        }
    }

    public static Map response(ResponseHeader header, Collection sources, IDataToMap dataToMap) {
        return response(header, dataToMaps(sources, dataToMap));
    }

    private static Map response(ResponseHeader header, Object source, IDataToMap dataToMap) {
        if (source == null) {
            return null;
        }
        Map map = new HashMap();
        dataToMap.dataToMap(map, source);
        return response(header, map);
    }

    private static Map response(ResponseHeader header) {
        return response(header, null);
    }

    public static Map fail(ResponseHeader header, String msg) {
        Map map = new HashMap();
        map.put(ApiDefinition.STATUS_KEY, header.getCode());
        map.put(ApiDefinition.MESSAGE_KEY,msg);
        map.put(ApiDefinition.BODY_KEY, null);
        return map;
    }


    private static Map response(ResponseHeader header, Object body) {
        Map map = new HashMap();
        map.put(ApiDefinition.STATUS_KEY, header.getCode());
        map.put(ApiDefinition.MESSAGE_KEY, header.getMessage());
        map.put(ApiDefinition.BODY_KEY, body);
        return map;
    }
    private static Map responseList(ResponseHeader header, Object body) {
        Map map = new HashMap();
        map.put(ApiDefinition.STATUS_KEY, header.getCode());
        map.put(ApiDefinition.MESSAGE_KEY, header.getMessage());
        map.put(ApiDefinition.BODY_KEY, new HashMap(){{
            put("list", body);
        }});
        return map;
    }


    private static Map response(ResponseHeader header, Object body, Integer total) {
        Map map = new HashMap();
        map.put(ApiDefinition.STATUS_KEY, header.getCode());
        map.put(ApiDefinition.MESSAGE_KEY, header.getMessage());
        map.put(ApiDefinition.BODY_KEY, body);
        return map;
    }

    public static Map response(ResponseHeader header, Object body, String message) {
        Map map = new HashMap();
        map.put(ApiDefinition.STATUS_KEY, header.getCode());
        map.put(ApiDefinition.MESSAGE_KEY, message);
        map.put(ApiDefinition.BODY_KEY, body);
        return map;
    }


    public static Map success() {
        return response(ResponseHeader.SUCCESS);
    }

    public static Map success(ResponseHeader header, Object body) {
        return response(header, body);
    }

    public static Map success(Object source, IDataToMap dataToMap) {
        return response(ResponseHeader.SUCCESS, source, dataToMap);
    }


    public static Map success(Object object) {
        return response(ResponseHeader.SUCCESS, object);
    }

    public static Map success(Object object, String message) {
        return response(ResponseHeader.SUCCESS, object, message);
    }



    public static Map success(Collection collection) {
        return response(ResponseHeader.SUCCESS, collection);
    }


    public static Map success(Collection source, IDataToMap dataToMap) {
        return response(ResponseHeader.SUCCESS, source, dataToMap);
    }


    public static Map successList(Collection source, IDataToMap dataToMap) {
        return responseList(ResponseHeader.SUCCESS, dataToMaps(source, dataToMap));
    }


    public static Map success(Object object,Integer total) {

        return response(ResponseHeader.SUCCESS, object,total);
    }

    public static Map fail() {
        return response(ResponseHeader.INTERNAL_SERVER_ERROR);
    }

    public static Map fail(String message) {
        return fail(ResponseHeader.INTERNAL_SERVER_ERROR, message);
    }


    public static Map fail(ResponseHeader header) {
        return response(header);
    }

    public static Map fail(ResponseHeader header, Object body) {
        return response(header, body);
    }

}
